package com.koron.css2.clientCenter.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.mapper.BusinessAreaMapper;
import com.koron.css2.clientCenter.mapper.ValveOpenMapper;
import com.koron.css2.clientCenter.queryBean.ValveOpenListQueryBean;
import com.koron.css2.clientCenter.vo.ValveOpenListVo;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 开阀管理列表
 *
 * @author cd
 * @author modify by 杨嘉慧
 */
public class ValveOpenList implements ServerInterface {
    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            ValveOpenListQueryBean queryBean = JsonUtils.objectToPojo(req.getData(), ValveOpenListQueryBean.class);

            ValveOpenMapper openMapper = factory.getMapper(ValveOpenMapper.class);
            PageHelper.startPage(queryBean.getPage(), queryBean.getPageCount());
            List<ValveOpenListVo> list = openMapper.selectList(queryBean);
            //获取阀门状态字典
            Map<String, String> dics = Tools.mapDicByCode(factory, "VMMVS");

            for (ValveOpenListVo vo : list) {
                //是否阀控表
                if (null != vo.getValveControlFlag() && "1".equals(vo.getValveControlFlag())) {
                    vo.setValveControlFlag("是");
                } else {
                    vo.setValveControlFlag("否");
                }
                //是否远程表
                if (null != vo.getRemoteFlag() && "1".equals(vo.getRemoteFlag())) {
                    vo.setRemoteFlag("是");
                } else {
                    vo.setRemoteFlag("否");
                }
                //通过字典表替换阀门状态为中文
                if (null != vo.getValveStatus() && null != dics.get(vo.getValveStatus())) {

                    String valveStatus = dics.get(vo.getValveStatus());
                    vo.setValveStatusName(valveStatus);
                }
                if (!StringUtils.isEmpty(vo.getBusinessArea())) {
                    BusinessAreaMapper businessAreaMapper = factory.getMapper(BusinessAreaMapper.class);
                    vo.setBusinessAreaName(
                            businessAreaMapper.findBusinessAreaById(vo.getBusinessArea(), userInfo.getCurWaterCode()));
                }
            }
            @SuppressWarnings("rawtypes")
            MessageBean<PageInfo> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "查询成功", PageInfo.class);
            info.setData(new PageInfo<>(list));
            return info;
        } catch (Exception e) {
            logger.error("开阀管理列表：" + e.getMessage(), e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "查询失败", null);
        }

    }

    public static void main(String[] args) {
        List<ValveOpenListVo> list = new ArrayList<>();
        list.add(new ValveOpenListVo());
        @SuppressWarnings("rawtypes")
        MessageBean<PageInfo> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "查询成功", PageInfo.class);
        info.setData(new PageInfo<>(list));
        System.out.println(JsonUtils.objectToJson(info));
    }
}
